<?php

/******************************************
 * File:			lib_files.php
 * Version:			1.0.0
 * Last modified:	25/10/2009
 * Last modifier:	Hugo Lavigne
 *****************************************/

Function get_folder_list($path, $complete=FALSE)
{
	if (($path == "") || (substr($path, -1) != "/"))
		return (-1);
  
	$handle = @opendir($path);
  
	$exit = false;
	$i = 0;
	$j = -1;
	while ($exit == false)
	{
		 @$file[$i] = readdir($handle);
		 if ($file[$i] == "")
			 $exit = true;
		 else
		 {
			 if (($file[$i]!=".")&&($file[$i]!=".."))
			 {
				 if (!is_dir($file[$i]."/"))
				 {
					 $j++;
					 if ($complete == TRUE)
						 $res[$j] = $path.$file[$i];
					 else
						 $res[$j] = $file[$i];
				 }
			 }
		 }     
		 $i++;
	}
	return ($res);
}
 
Function get_file_list($path, $complete=FALSE)
{
	if (($path == "") || (substr($path, -1) != "/"))
		return (-1);
  
	$handle = @opendir($path);
	$exit = false;
	$i = 0;
	$j = -1;
	while ($exit == false)
	{
		@$file[$i] = readdir($handle);
		 if ($file[$i] == "")
			 $exit = true;
		 else
		 {
			 if (($file[$i]!=".")&&($file[$i]!=".."))
			 {
				 if (!is_dir($file[$i]."/"))
				 {
					 $j++;
					 if ($complete == TRUE)
						 $res[$j] = $path.$file[$i];
					 else
						 $res[$j] = $file[$i];
				 }
			 }
		 }     
		 $i++;
	}
	return ($res);
 }

Function get_size($size, $unit=FALSE)
{
	$size = $size / 1024 / 1024;
	
	if ($size < 0.001)
	{
		while ($size < 1)
		{
			$size *= 1000;
		}
		$size = round($size);
		if ($unit == TRUE)
			$size = $size." Oc";
	}
	elseif ($size < 1)
	{
		while ($size < 1)
		{
			$size *= 1000;
		}
		$size = round($size);
		if ($unit == TRUE)
			$size = $size." Ko";
	}
	elseif (($size) > 999 && ($size < 1000000))
	{
		while ($size > 999)
		{
			$size /= 1000;
		}
		$size = round($size);
		if ($unit == TRUE)
			$size = $size." Go";
	}
	elseif ($size > 99999)
	{
		while ($size > 999)
		{
			$size /= 1000;
		}
		$size = round($size);
		if ($unit == TRUE)
			$size = $size." To";
	}
	else
	{
		$size = round($size);
		if ($unit == TRUE)
			$size = $size." Mo";
	}
	
	return ($size);
}

Function get_fileext($file)
{
	$ext = explode('.', $file);
	$nb = count($ext) - 1;
	return($file_ext = $ext[$nb]);
}

Function get_filename($file)
{
	$tmp = explode('.', $file);
	return($file_name = $tmp[0]);
}

?>